package net.jblog.struts.backoffice.action;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.jblog.business.IConstantes;
import net.jblog.business.ServiceLocatorReference;
import net.jblog.business.facade.CategoryFacade;
import net.jblog.data.model.Category;
import net.jblog.struts.backoffice.form.CategoryForm;
import net.jblog.struts.tools.BeanUtils;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class ListCategoriesAction extends Action implements IConstantes {
	@SuppressWarnings("unused")
	private Logger log = Logger.getLogger(ListCategoriesAction.class.getName());
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
		HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		CategoryFacade facade = ServiceLocatorReference.getServiceLocator().getCategoryFacade();
		List<Category> categories = facade.findByCriteria(0, 10, null, false);
		
		List<CategoryForm> categoryForms = new ArrayList<CategoryForm>();
		for (Category category : categories) {
			CategoryForm categoryForm = new CategoryForm();
			BeanUtils.copyPropertiesFromCategoryBeanToCategoryForm(category, categoryForm);
			categoryForms.add(categoryForm);
		}
		request.setAttribute("categories", categoryForms);
		
		return mapping.findForward("success");
	}

}
